/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : app_freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "usr_inc.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
  .name = "defaultTask",
  .priority = (osPriority_t) osPriorityLow,
  .stack_size = 400 * 4
};
/* Definitions for chassisUpdate */
osThreadId_t chassisUpdateHandle;
const osThreadAttr_t chassisUpdate_attributes = {
  .name = "chassisUpdate",
  .priority = (osPriority_t) osPriorityHigh,
  .stack_size = 300 * 4
};
/* Definitions for leftUpExe */
osThreadId_t leftUpExeHandle;
const osThreadAttr_t leftUpExe_attributes = {
  .name = "leftUpExe",
  .priority = (osPriority_t) osPriorityNormal,
  .stack_size = 400 * 4
};
/* Definitions for chassisPrintf */
osThreadId_t chassisPrintfHandle;
const osThreadAttr_t chassisPrintf_attributes = {
  .name = "chassisPrintf",
  .priority = (osPriority_t) osPriorityLow7,
  .stack_size = 400 * 4
};
/* Definitions for leftDownExe */
osThreadId_t leftDownExeHandle;
const osThreadAttr_t leftDownExe_attributes = {
  .name = "leftDownExe",
  .priority = (osPriority_t) osPriorityNormal,
  .stack_size = 400 * 4
};
/* Definitions for rightDownExe */
osThreadId_t rightDownExeHandle;
const osThreadAttr_t rightDownExe_attributes = {
  .name = "rightDownExe",
  .priority = (osPriority_t) osPriorityNormal,
  .stack_size = 400 * 4
};
/* Definitions for rightUpExe */
osThreadId_t rightUpExeHandle;
const osThreadAttr_t rightUpExe_attributes = {
  .name = "rightUpExe",
  .priority = (osPriority_t) osPriorityNormal,
  .stack_size = 400 * 4
};
/* Definitions for dgmErrorHandle */
osThreadId_t dgmErrorHandleHandle;
const osThreadAttr_t dgmErrorHandle_attributes = {
  .name = "dgmErrorHandle",
  .priority = (osPriority_t) osPriorityNormal,
  .stack_size = 400 * 4
};
/* Definitions for pidArgsQueue */
osMessageQueueId_t pidArgsQueueHandle;
const osMessageQueueAttr_t pidArgsQueue_attributes = {
  .name = "pidArgsQueue"
};
/* Definitions for spdCmdQueue */
osMessageQueueId_t spdCmdQueueHandle;
const osMessageQueueAttr_t spdCmdQueue_attributes = {
  .name = "spdCmdQueue"
};
/* Definitions for gm1InfoBox */
osMessageQueueId_t gm1InfoBoxHandle;
const osMessageQueueAttr_t gm1InfoBox_attributes = {
  .name = "gm1InfoBox"
};
/* Definitions for gm2InfoBox */
osMessageQueueId_t gm2InfoBoxHandle;
const osMessageQueueAttr_t gm2InfoBox_attributes = {
  .name = "gm2InfoBox"
};
/* Definitions for gm3InfoBox */
osMessageQueueId_t gm3InfoBoxHandle;
const osMessageQueueAttr_t gm3InfoBox_attributes = {
  .name = "gm3InfoBox"
};
/* Definitions for gm4InfoBox */
osMessageQueueId_t gm4InfoBoxHandle;
const osMessageQueueAttr_t gm4InfoBox_attributes = {
  .name = "gm4InfoBox"
};
/* Definitions for dgmStatuswordMsgQueue */
osMessageQueueId_t dgmStatuswordMsgQueueHandle;
const osMessageQueueAttr_t dgmStatuswordMsgQueue_attributes = {
  .name = "dgmStatuswordMsgQueue"
};
/* Definitions for djiMutex */
osMutexId_t djiMutexHandle;
const osMutexAttr_t djiMutex_attributes = {
  .name = "djiMutex"
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void *argument);
extern void StartChassisUpdate(void *argument);
extern void StartLeftUpExe(void *argument);
extern void StartChassisPrintf(void *argument);
extern void StartLeftDownExe(void *argument);
extern void StartRightDownExe(void *argument);
extern void StartRightUpExe(void *argument);
extern void StartDgmErrorHandle(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */
  /* Create the mutex(es) */
  /* creation of djiMutex */
  djiMutexHandle = osMutexNew(&djiMutex_attributes);

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* Create the queue(s) */
  /* creation of pidArgsQueue */
  pidArgsQueueHandle = osMessageQueueNew (8, sizeof(PidArgsMsg_t), &pidArgsQueue_attributes);

  /* creation of spdCmdQueue */
  spdCmdQueueHandle = osMessageQueueNew (16, sizeof(ChassisSpdCmdMsg_t), &spdCmdQueue_attributes);

  /* creation of gm1InfoBox */
  gm1InfoBoxHandle = osMessageQueueNew (1, sizeof(GM6020FeedbackMsg_t), &gm1InfoBox_attributes);

  /* creation of gm2InfoBox */
  gm2InfoBoxHandle = osMessageQueueNew (1, sizeof(GM6020FeedbackMsg_t), &gm2InfoBox_attributes);

  /* creation of gm3InfoBox */
  gm3InfoBoxHandle = osMessageQueueNew (1, sizeof(GM6020FeedbackMsg_t), &gm3InfoBox_attributes);

  /* creation of gm4InfoBox */
  gm4InfoBoxHandle = osMessageQueueNew (1, sizeof(GM6020FeedbackMsg_t), &gm4InfoBox_attributes);

  /* creation of dgmStatuswordMsgQueue */
  dgmStatuswordMsgQueueHandle = osMessageQueueNew (16, sizeof(DgmStatuswordMsg_t), &dgmStatuswordMsgQueue_attributes);

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of defaultTask */
  defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

  /* creation of chassisUpdate */
  chassisUpdateHandle = osThreadNew(StartChassisUpdate, NULL, &chassisUpdate_attributes);

  /* creation of leftUpExe */
  leftUpExeHandle = osThreadNew(StartLeftUpExe, NULL, &leftUpExe_attributes);

  /* creation of chassisPrintf */
  chassisPrintfHandle = osThreadNew(StartChassisPrintf, NULL, &chassisPrintf_attributes);

  /* creation of leftDownExe */
  leftDownExeHandle = osThreadNew(StartLeftDownExe, NULL, &leftDownExe_attributes);

  /* creation of rightDownExe */
  rightDownExeHandle = osThreadNew(StartRightDownExe, NULL, &rightDownExe_attributes);

  /* creation of rightUpExe */
  rightUpExeHandle = osThreadNew(StartRightUpExe, NULL, &rightUpExe_attributes);

  /* creation of dgmErrorHandle */
  dgmErrorHandleHandle = osThreadNew(StartDgmErrorHandle, NULL, &dgmErrorHandle_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */
__weak void StartDefaultTask(void *argument)
{
  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END StartDefaultTask */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

